 <template>
   <el-card shadow="always" class="box-card">
      <div class="card-name search-item">
        <div class="name width2">城市数据</div>
        <div class="mr-30">
          <el-button size="medium" type="primary" @click="add">添加城市</el-button>
        </div>
      </div>
      <div class="content-con">
        <div class="form-item">
          <el-table
            :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
            class="table"
            stripe border
            :header-cell-style="{background:'#f3f3f3',textAlign: 'center'}"
            :cell-style="{ textAlign: 'center' }">
            <el-table-column prop="city_no" label="编号" width="140">
            </el-table-column>
            <el-table-column prop="superior_name" label="上级名称">
            </el-table-column>
            <el-table-column prop="area_name" label="地区名称">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="text" @click="handleEdit(scope.row)">
                  <i class="el-icon-edit"></i>编辑
                </el-button>
                <el-button type="text" @click.native.prevent="deleteRow(scope.$index,tableData)">
                  <i class="el-icon-delete"></i>删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 分页-页码 -->
        <div class="page">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[50, 100, 150, 200]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="tableData.length">
          </el-pagination>
        </div>
      </div>
      <city-edit ref="CityEdit" :rows="rows"></city-edit>
   </el-card>

 </template>
<script>
import CityEdit from '@/components/logistics/CityEdit';
export default {
  components:{
    CityEdit
  },
  data () {
    return {
      rows: '',
      currentPage: 1, //当前页码
      pagesize: 50,  //每页的数据条数
      tableData: [
        {
          city_no: '1',
          superior_name: '中国',
          area_name: '北京市',
          merge_name: '北京',
          area_code: '110000000000',
          longitude: '116.407526',
          latitude: '39.904030'
        },
        {
          city_no: '2',
          superior_name: '中国',
          area_name: '天津市',
          merge_name: '天津',
          area_code: '120000000000',
          longitude: '117.200983',
          latitude: '39.084158'
        },
        {
          city_no: '3',
          superior_name: '中国',
          area_name: '河北省',
          merge_name: '河北',
          area_code: '130000000000',
          longitude: '114.468664',
          latitude: '38.037057'
        }
      ]
    }
  },

  methods: {
    //搜索
    search() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          console.log(this.form)
          this.$message.success('成功!');
        } else {
          console.log('error submit!!');
          this.$message.error('失败!');
          return false;
        }
      });
    },

    // 新增
    add(){
      this.$nextTick(()=>{
        this.$refs.CityEdit.openAdd();
      });
    },

    //编辑操作
    handleEdit(row){
      this.rows = row;
      this.$nextTick(()=>{
        this.$refs.CityEdit.openEdit();
      });
    },

    //删除操作
    deleteRow(index, rows){
      rows.splice(index, 1);
    },

    //每页条数改变时触发事件,选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pagesize = val;
    },
    // 当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    }
  }
}
</script>
<style lang="scss">

</style>
